<section>
<script>
	var xmlstr;
	var roles;
	function validatePassword(pass1,pass2){
		if(pass1 == pass2)
			return true;
		else
			return false;
	}
	
	function resetFields(){
		$("input[value='student']").attr("checked","checked");
		$("#bulkUsername").val("");
		$("#rangeFrom").val("");
		$("#rangeTo").val("");
		$("#usernameList").val("");
		$("#bulkPassword").val("");
		$("#bulkrenter").val("");	
	}
	
	
	function padNumber(num,len,size) {
		var paddednum="";
		while(len!=size)
		{
			paddednum=paddednum+"0";
			len++;
		}
		
		return paddednum+num;
	}
	
	function listOfUsernames(prefix,rangefrom,rangeto){
		var i;
		var j=0;
		count=rangeto-rangefrom+1;
		var username=new Array();
		var usernamestr="";
		var y=parseInt(rangeto);
		var len2=""+rangeto.length;
		xmlstr= "<usernames>";
		for(i=parseInt(rangefrom);i<=y;i++)
		{
			var x=""+i;
			len1=x.length;
			var number="";
			if(len1<len2)
			{
				number=padNumber(i,len1,len2);
			}
			else
				number=i;
			username[j]=prefix+number;
			usernamestr=usernamestr+username[j]+"\n";
			xmlstr=xmlstr+"<user>"+username[j]+"</user>";
			j++;
		}
		xmlstr=xmlstr+"</usernames>";
		return usernamestr;
	}
	
	$(function(){
		$("#bulkrenter").blur(function(){
			var pass1=$("#bulkPassword").val();
			var pass2=$("#bulkrenter").val();
			if(!validatePassword(pass1,pass2))
			{
				$(":password").val("");
				$("#result").html("Passwords doesnot match.");
				
			}
			else
				$("#result").val("");
		});
		
		$("#bulkPassword").focus(function(){
			$("#result").html("");
		});
		
		$("#rangeTo").blur(function(){
			var pre=$("#bulkUsername").val();
			var rfrom=$("#rangeFrom").val();
			var rto=$("#rangeTo").val();
			names=listOfUsernames(pre,rfrom,rto);
			$("#usernameList").val(names);
		});
		
		$("#createUsers").click(function(){
			roles=$("input:radio[name=usertype]:checked").val();
			$.post("create_bulk_users.php",{
				users:xmlstr,
				role:roles,
				pass:$("#bulkPassword").val()
			},function(data){
				alert(data);
				resetFields();
			});
		});
		
		$("#bulkreset").click(function(){
			resetFields();
		});
		
		$("#cancel").click(function(){
			$("#content").load("./welcome.html");
		});
	});
	
</script>
<section class="adminforms" id="admincontent">
	Role:<input type="radio" name="usertype" value="student" checked="checked" class="admin_form">Student
	<input type="radio" name="usertype" value="staff" class="admin_form">Staff<br/><br/>
	Username<br/><input type="text"  id="bulkUsername"class="admin_form"/><br/><br/>
	Range<br/>
	<input type="text" name="rangeFrom" id="rangeFrom" placeholder="From" class="admin_form"/><br/><br/><input type="text" name="rangeTo" id="rangeTo" placeholder="To" class="admin_form"/><br/><br/>
	List of usernames<br/>
	<textarea cols="53" rows="5" id="usernameList"></textarea><br/><br/>
	Password<br/><input type="password" id="bulkPassword" name="bulkPassword" class="admin_form" /><br/><br/>
	Confirm Password<br/><input type="password" id="bulkrenter" name="bulkrenter" class="admin_form"/>&nbsp;&nbsp;<label id="result" name="result" class="resultlabel"></label><br/>
	<button type="button" id="createUsers">Create Users</button><button type="button" id="bulkreset">Reset</button><button type="button" id="cancel">Cancel</button>
</section>

